﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

namespace WebSite.admin.product
{
    public partial class goods_add06 : BaseAdmin
    {
        // Fields

        protected string strAreaData;
        public string typename = "";

        // Methods
        private void AddItemFromSourceListBox(ListBox sourceBox, ListBox targetBox, string aa)
        {
            string str = "0";
            foreach (ListItem item in targetBox.Items)
            {
                if (sourceBox.SelectedValue.ToString() == item.Value.ToString())
                {
                    str = "1";
                    break;
                }
            }
            if (aa.ToString() == "1")
            {
                foreach (ListItem item2 in sourceBox.Items)
                {
                    if ((item2.Selected && !targetBox.Items.Contains(item2)) && (str.ToString() == "0"))
                    {
                        targetBox.Items.Add(new ListItem(item2 + "", "" + sourceBox.SelectedValue.ToString() + ""));
                    }
                }
            }
            else
            {
                foreach (ListItem item3 in sourceBox.Items)
                {
                    if (!targetBox.Items.Contains(item3) && (str.ToString() == "0"))
                    {
                        targetBox.Items.Add(new ListItem(item3 + "", "" + item3.Value + ""));
                    }
                }
            }
        }

        protected void btnToAllLeft_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in this.lbRight.Items)
            {
                SQLAccess.DoSql("delete from DB_Propei where proids=" + item.Value + " and  proid=" + base.Request.QueryString["gid"].ToString() + "");
            }
            this.RemoveSelectedItemAll(this.lbRight);
        }

        protected void btnToAllRight_Click(object sender, EventArgs e)
        {
            string aa = "";
            if (this.lbLeft.Items.Count > 0)
            {
                this.AddItemFromSourceListBox(this.lbLeft, this.lbRight, aa);
            }
        }

        protected void btnToLeft_Click(object sender, EventArgs e)
        {
            if (this.lbRight.SelectedItem != null)
            {
                SQLAccess.DoSql("delete from DB_Propei where proids=" + this.lbRight.SelectedValue.ToString() + " and  proid=" + base.Request.QueryString["gid"].ToString() + "");
                this.RemoveSelectedItem(this.lbRight);
            }
        }

        protected void btnToRight_Click(object sender, EventArgs e)
        {
            string aa = "1";
            if (this.lbLeft.SelectedItem != null)
            {
                this.AddItemFromSourceListBox(this.lbLeft, this.lbRight, aa);
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (base.Request.QueryString["pid"] != null)
            {
                for (int i = 0; i < this.lbRight.Items.Count; i++)
                {
                    string[] strArray = this.lbRight.Items[i].Text.ToString().Split(new char[] { '-' });
                    string sql = "Select * From DB_Propei Where proid =" + base.Request.QueryString["gid"].ToString() + " and proids=" + this.lbRight.Items[i].Value.ToString() + "";
                    DataTable table = new DataTable();
                    table = SQLAccess.GreatDs(sql).Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        SQLAccess.DoSql("delete from DB_Propei where propeiid=" + table.Rows[0]["propeiid"].ToString() + "");
                    }
                    SQLAccess.DoSql("insert into DB_Propei (proid,proids,pronames) values (" + base.Request.QueryString["gid"].ToString() + "," + this.lbRight.Items[i].Value.ToString() + ",'" + strArray[0].ToString() + "')");
                }
                base.Response.Write("<script language='javascript'>window.location.href='goods_add06.aspx?gid=" + base.Request.QueryString["gid"] + "';</script>");
                base.Response.End();
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            string str = "0";
            string sql = "select * from DB_Pinpai where visible=1  ";
            if (this.typeid.SelectedValue.ToString() != "0")
            {
                sql = sql + " and pinpaiid=" + this.typeid.SelectedValue.ToString() + "";
            }
            if (re.replace(this.keyname.Value.ToString()) != "")
            {
                sql = sql + " and pinpainame like '%" + re.replace(this.keyname.Value.ToString()) + "%'";
            }
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    foreach (ListItem item in this.lbLeft.Items)
                    {
                        if (row["pinpaiid"].ToString() == item.Value.ToString())
                        {
                            str = "1";
                            break;
                        }
                    }
                    if (str == "0")
                    {
                        this.lbLeft.Items.Add(new ListItem(row["pinpainame"].ToString(), row["pinpaiid"].ToString()));
                        str = "0";
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lbLeft.Attributes.Add("onchange", "changearea1()");
            this.strAreaData = "<script language = 'JavaScript' type='text/javascript'>var onecount; onecount=0; subcat = new Array(); ";
            string sql = "select * from DB_Pinpai where visible=1";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            int num = 0;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                string strAreaData = this.strAreaData;
                this.strAreaData = strAreaData + "subcat[" + i.ToString() + "] = new Array('" + table.Rows[i][1].ToString().Trim() + "','" + table.Rows[i][0].ToString().Trim() + "'); ";
                num++;
            }
            this.strAreaData = this.strAreaData + "onecount=" + num.ToString() + "; </script>";
            this.ImageButton1.Attributes["onclick"] = "return clicka();";
            if (base.Request.QueryString["gid"] != null)
            {
                DataTable table2 = SQLAccess.GreatDs("select typeName from tbGoodsType where typeId=" + base.Request.QueryString["gid"] + "").Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.typename = table2.Rows[0][0].ToString();
                }
            }
            if (!base.IsPostBack)
            {
                if (base.Request.QueryString["gid"] != null)
                {
                    this.upbind();
                }
                string str3 = "Select * From DB_Pinpai Where visible=1 order By pinpaiorder";
                DataTable table3 = new DataTable();
                table3 = SQLAccess.GreatDs(str3).Tables[0];
                if (table3.Rows.Count > 0)
                {
                    foreach (DataRow row in table3.Rows)
                    {
                        this.typeid.Items.Add(new ListItem("" + row["pinpainame"].ToString() + "", "" + row["pinpaiid"].ToString() + ""));
                    }
                }
            }
        }

        private void RemoveSelectedItem(ListBox listControl)
        {
            while (listControl.SelectedIndex != -1)
            {
                listControl.Items.RemoveAt(listControl.SelectedIndex);
            }
        }

        private void RemoveSelectedItemAll(ListBox listControl)
        {
            if (listControl.Items.Count > 0)
            {
                listControl.Items.Clear();
            }
        }

        private void upbind()
        {
            string sql = "select * from DB_Propei where proid=" + base.Request.QueryString["gid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.lbRight.Items.Add(new ListItem(row["pronames"].ToString() + "", row["proids"].ToString()));
                }
            }
        }




    }
}